<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE html>
<html>
<head>
<base href="<%=basePath%>">

<title>发表文章</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<script src="jquery/jquery-3.1.1.js"></script>
<script type="text/javascript" src="ckeditor/ckeditor.js"></script>
<link rel="stylesheet" href="css/StyleSheet.css" type="text/css" />
<style type="text/css">
table {
	background-color: white;
}

#txtTitle,#txtSummary {
	background-color: #CBEACB;
}

#txtSummary {
	overflow: scroll;
}
</style>

</head>

<body>
	<table class="regTable">
		<tr>
			<td class="mytd1" style="width:100px"><label for="txtTitle">标题：</label>
			</td>
			<td class="mytd"><input type="text" name="txtTitle"
				id="txtTitle" Style="width: 200px" class="input1" required />
			</td>
		</tr>
		<tr>
			<td class="mytd1"><label for="txtSummary">摘要：</label>
			</td>
			<td class="mytd"><textarea name="txtSummary" id="txtSummary"
					Style="width: 400px; height:120px" class="input1" value="" /></textarea>
			</td>
		</tr>
		<tr>
			<td class="mytd1"><label for="txtContent">内容：</label>
			</td>
			<td class="mytd"><textarea name="txtContent" id="txtContent"
					Style=" height:375px;width: 400px;" value="" /></textarea> <script
					type="text/javascript">
						var editor = null;
						window.onload = function() {
							editor = CKEDITOR.replace('txtContent');
						}
					</script>
			</td>
		</tr>
		<tr>
			<td class="mytd1">分类：</td>
			<td class="mytd categories"></td>
		</tr>
		<tr>
			<td class="mytd1">设置：</td>
			<td class="mytd"><input type="checkbox" name="chkIsPublished"
				id="chkIsPublished" value="" /><label for="chkIsPublished">发布</label>&nbsp; <input type="checkbox"
				name="chkIsCommentEnabled" id="chkIsCommentEnabled" value="" /><label for="chkIsCommentEnabled">允许评论</label>
			</td>
		</tr>
		<tr>
			<td colspan="2" align="center" class="mytd"><input type="button"
				name="bntSave" id="bntSave" value="保存" class="opt_sub"
				onclick="save()" />
			</td>
		</tr>
	</table>
	<script type="text/javascript">
		InitCategories();
		/* 设置标题 */
		var time = new Date();
		var y = time.getFullYear();
		var m = time.getMonth() + 1;
		var d = time.getDate();
		var h = time.getHours();
		var min = time.getMinutes();
		var s = time.getSeconds();
		var str = y + "-" + m + "-" + d + ' ' + h + ":" + min + ':' + s;
		$('#txtTitle').val(str);
		var title;
		var summary;
		var categoryid;
		var ispublished;
		var iscommentenabled;
		var c;
		/* 保存 */
		function save() {
			c = editor.getData();
			editor.updateElement();
			title = $("#txtTitle").val();
			summary = $("#txtSummary").val();
			categoryid = $('input:radio:checked').val();
			ispublished = $("#chkIsPublished").is(':checked');
			iscommentenabled = $("#chkIsCommentEnabled").is(':checked');
 			console.log(c+";"+title+";"+summary+";"+categoryid+";"+ispublished+";"+iscommentenabled);
			if (IsNull(title)) {
				alert("请填写文章标题!");
				return;
			}
			if (IsNull(summary)) {
				alert("请填写文章摘要!");
				return;
			}
			if (IsNull(c)) {
				alert("请填写文章内容!");
				return;
			}
			$.ajax({
				type : "POST",
				url : "postEdit!setPost",
				dataType : "text",
				contentType : "text",
				data : {
					"p.title" : title,
					"p.summary" : summary,
					"p.postcontent" : c,
					"p.categoryid" : categoryid,
					"p.ispublished" : ispublished,
					"p.iscommentenabled" : iscommentenabled
				},
				success : function(msg) {
				alert(msg);
					
				},
				error : function(r, r2, err) {
					alert("连接错误:" + err.message);
				}
			});
		}
		/* 初始化博客分类信息  */
		function InitCategories() {
			$.ajax({
				type : "POST",
				url : "categories",
				success : function(msg) {
					var json = eval("(" + msg + ")");
					for ( var i = 0; i < json.length; i++) {
						$(".categories").append(
								"<input type='radio' name='a' checked value='"+json[i].name+"' />"
										+ json[i].name + "");
					}
				},
				error : function(r, r2, err) {
					alert("连接错误:" + err.message);
				}
			});
		}
		function IsNull(str) {
			if (str.length == 0) {
				return true;
			} else
				return false;
		}
	</script>
</body>
</html>

